﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets; 
namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
       
        private void GetPublicIP()
        {
            WebRequest wr = WebRequest.Create("http://checkip.dyndns.org/");
            WebResponse wr2 = wr.GetResponse();
            StreamReader sr = new StreamReader(wr2.GetResponseStream());
            string GlobalIP = sr.ReadToEnd();
            int first = GlobalIP.IndexOf("s: ") + 3;
            int last = GlobalIP.IndexOf("</b");
            int length = last - first;
            GlobalIP = GlobalIP.Substring(first, length);
            tbIPGlobal.Text = GlobalIP;
        }
        private void GetLocalHostName()
        {
            string HostName = Dns.GetHostName();
            tbName.Text = HostName; 
           
        }
        private void GetLocalIP()
        {
            IPHostEntry local = Dns.GetHostEntry(tbName.Text);
            for (int index = 0; index < local.AddressList.Length; index++)
            {
                string localIP = Convert.ToString(local.AddressList[index]);
                rtbLocal.Text += localIP;
                rtbLocal.Text += "/n";
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            GetLocalHostName();
            GetPublicIP();
            GetLocalIP();
        }

        //--------------------ЧАТ-----------------------------
        TcpListener listener;
        TcpClient client;
        StreamReader reader;
        string message;
        NetworkStream readstream;

        private void StartListening()

        {
            int port = Convert.ToInt32(tbMyPort.Text);
            listener = new TcpListener(IPAddress.Any, port);
            listener.Start();
            bw.RunWorkerAsync();

        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            StartListening();

        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            client = listener.AcceptTcpClient();
            reader = new StreamReader(client.GetStream());
            message = reader.ReadToEnd(); reader.Close(); 

        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lbMessages.Items.Add(">" + message);
            bw.RunWorkerAsync(); 
        }
        private void send()
        {
            try
            {
                TcpClient sender = new TcpClient();
                int port = Convert.ToInt32(tbSenderPort.Text);
                sender.Connect(IPAddress.Parse(tbSenderIP.Text), port);
                StreamWriter write = new StreamWriter(sender.GetStream());
                String current_time_str = DateTime.Now.ToString("HH:mm:ss");
                write.WriteLine(tbMessage.Text );
                write.WriteLine( current_time_str);
                write.WriteLine(tbYourName.Text);
                write.Close(); 


            }
            catch
            {

            }


        }

        private void Send_Click(object sender, EventArgs e)
        {
            send(); 
        }


     

      
    }
}
